using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;
using PMSBusiness.Core.Toolkit;
using AppToolKit.Core.Context.Spring;
using Common.Logging;
using PMSBusiness.Core.Logger;
using PMSBusiness.Core.Toolkit;
using PMSBusiness.Toolkit;

namespace PMSBusiness.Service.Background
{
    [AutoWiring]
    public class WorkTaskLoader
    {
        [AutoWiring]
        public ContextAware Context { get; set; }
        [AutoWiring]
        public WorkTaskExecutor WorkTaskExecutor{get;set;}
        private static ILog logger_ = LogManager.GetLogger(LoggerFixure.LoggerConfigure.DefaultLogger);

     

        public void   Load()
        {            

            XmlReadWriteHelper xmlHelp = new XmlReadWriteHelper(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\Config\\WorkTask\\WorkTask.xml");
            XmlNodeList xmlSets = xmlHelp.GetNodes("./Root/WorkTaskSet");
          
            for (int i = 0; i < xmlSets.Count; i++)
            {
                string strWorkSetType;
                XmlNodeList xmlTasks ;

                if (xmlSets[i].HasChildNodes)
                {
                    strWorkSetType = xmlHelp.GetSubNodeValue(xmlSets[i], "Name");
                                
                    WorkTaskSet workTaskSetdb = new WorkTaskSet(strWorkSetType);

                    xmlTasks = xmlSets[i].SelectNodes("Task");
                    
                    for (int j = 0; j < xmlTasks.Count; j++)
                    {
                    	string type=XmlNodeHelper.GetNodeAttributeValue(xmlTasks[j],"Type");
                    	IWorkTask task =(IWorkTask)Context.Create(Type.GetType(type));                    	
                        task.ReadConfigure(xmlTasks[j]); 
                        workTaskSetdb.Add(task);
                    }                        
                 
                    WorkTaskExecutor.Add(workTaskSetdb);
                }
            }

            
        }
    }
}
